package Engine;

import LinkDB.Conexao;
import Repositorio.RepositorioOs;
import View.Load;
import View.Novo;
import View.Ver;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

public class Motor {

    Novo novo = new Novo();

    public void carregandoSistema() {
        new Thread() {
            @Override
            public void run() {
                try {
                    sleep(3000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                }
                for (int i = 0; i <= 100; i++) {
                    Load.load.setValue(i);
                    try {
                        sleep(80);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    if (i <= 10) {
                        Load.status.setText("Iniciando . . .");
                    } else if (i >= 11 && i <= 30) {
                        Load.status.setText("Conectando-se ao banco . . .");
                        if (Conexao.getConnectionIsValid() == true) {
                            Load.status.setText("Conectando-se ao banco . . .");
                            try {
                                Conexao.getConexao().close();
                            } catch (SQLException ex) {
                                Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        } else {
                            Load.status.setText("Erro ao conectar-se ao banco.");
                            try {
                                Conexao.getConexao().close();
                            } catch (SQLException ex) {
                                Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            break;
                        }
                        try {
                            Conexao.getConexao().close();                            
                        } catch (SQLException ex) {
                            Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                    
                    if(i == 100){
                        Load.status.setText("Pronto");
                            Load.C1.setEnabled(true);
                            Load.novo.setVisible(true);
                            Load.C1.setVisible(true);
                            Load.C2.setEnabled(true);
                            Load.ver.setVisible(true);
                            Load.C2.setVisible(true);
                            Load.C3.setEnabled(true);
                            Load.finalizar.setVisible(true);
                            Load.C3.setVisible(true);                            
                    }
                }
            }
        }.start();

    }
    
    public void carregaChamadosAbertos(){
        
        new Thread(){
            @Override
            public void run(){
                Repositorio.RepositorioOs repoOs = new RepositorioOs();
                DefaultTableModel model = (DefaultTableModel) Ver.jTable1.getModel();
                try {
                    repoOs.verChamadosAbertos();
                } catch (SQLException ex) {
                    Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                }
                while(true){
                    try {
                        sleep(30000);
                        try {
                            model.setRowCount(0);
                            repoOs.verChamadosAbertos();
                        } catch (SQLException ex) {
                            Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Motor.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }.start();
    }
}
